package cn.rkylin.oms.warehouse.shiporder.util;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import cn.rkylin.oms.common.consts.YesNoConst;
import cn.rkylin.oms.order.service.IOrderService;
import cn.rkylin.oms.warehouse.local.returnStockin.service.IReturnStockinService;
import cn.rkylin.oms.warehouse.local.returnStockin.vo.ReturnStockinSkuVO;
import cn.rkylin.oms.warehouse.shiporder.service.IShiporderService;
import cn.rkylin.oms.warehouse.shiporder.vo.ShiporderSkuVO;

@Component("snCodeExtractor")
public class SnCodeExtractor {
    /**
     * shipOrderService:发货单查询服务——提供订单发货单和补发发货单服务.
     */
    @Autowired
    IShiporderService shipOrderService;

    /**
     * returnStockInService:退货入库单服务.
     */
    @Autowired
    IReturnStockinService returnStockInService;

    /**
     * getSnCodeBySourceSkuId: 取发货商品的SN号. <br/>
     *
     * @author zhanghao
     * @param string 
     * @param orderSkuId
     *            商品的主键ID
     * @param Type
     *            商品的类型：or 订单发货商品；rs 补发单发货商品
     * @param sourceId 订单或者补发单
     * @return
     * @throws Exception
     */
    public String getSnCodeBySourceSkuId(String sourceSkuId, String Type, String sourceId) throws Exception {
        ShiporderSkuVO shiporderSkuVO = new ShiporderSkuVO();
        shiporderSkuVO.setOrderSkuId(sourceSkuId);
        if(StringUtils.isNotEmpty(Type)&&("or".equalsIgnoreCase(Type)||"rs".equalsIgnoreCase(Type))) {
            shiporderSkuVO.setShipOrderType(Type.toLowerCase());
            shiporderSkuVO.setShipOrderSourceId(sourceId);
        }
        List<ShiporderSkuVO> shiporderSkuList = shipOrderService.selectShiporderSkuList(shiporderSkuVO);

        List<String> snList = new ArrayList<String>();
        if (shiporderSkuList.size() > 0) {
            for (ShiporderSkuVO shiporderSkuVO2 : shiporderSkuList) {
                if (StringUtils.isNotEmpty(shiporderSkuVO2.getSn())) {
                    snList.add(shiporderSkuVO2.getSn());
                }
            }
        }

        return StringUtils.join(snList.toArray(), ",");
    }

    /**
     * getSnCodeByReturnSkuId:取退货入库商品的SN号. <br/>
     *
     * @author zhanghao
     * @param sourceSkuId
     *            退货单商品
     * @return
     * @throws Exception
     */
    public String getSnCodeByReturnSkuId(String sourceSkuId) throws Exception {
        ReturnStockinSkuVO returnStockinSkuVO = new ReturnStockinSkuVO();
        returnStockinSkuVO.setSourceSkuId(sourceSkuId);
        List<ReturnStockinSkuVO> returnStockInSkuList = returnStockInService.selectReturnStockinSkuList(returnStockinSkuVO);
        if (returnStockInSkuList.size() > 0) {
            return returnStockInSkuList.get(0).getSn();
        }
        return "";
    }
}
